function BookmarkView() {
	var DbHelper = require('ui/common/BookmarksDbHelper');
	var dbHelper = new DbHelper();
	
	//create object instance, parasitic subclass of Observable
	var self = Ti.UI.createView({
		backgroundColor : 'white'
	});

	//search bar
	var bookmarkSearch = Titanium.UI.createSearchBar({
		showCancel : false,
		hintText: 'Search in English'
	});

	var lang = "mal";

	var dbResult = dbHelper.getRows("mal");
	var tableData = dbResult.data;
	var index = dbResult.index;
	
	var table = Ti.UI.createTableView({
		backgroundColor:'#D3D3D3',
		data: tableData,
		search : bookmarkSearch,
		searchHidden : true,
		filterAttribute : 'filter',
		index: index,
  		separatorStyle: Titanium.UI.iPhone.TableViewStyle.GROUPED,
  		editable: true,
  		bottom: 44
	});
	self.add(table);

	//add behavior
	table.addEventListener('click', function(e) {
		if(!e.rowData.ml) {
			return;
		}
		self.fireEvent('itemSelected', {
			ml: e.rowData.ml,
			en: e.rowData.en,
			title_ml : e.rowData.title_ml,
			title_en: e.rowData.filter
		});
	});
	
	table.addEventListener('delete', function(e) {
		if(!e.rowData.ml) {
			return;
		}
		dbHelper.deleteRow({ml: e.rowData.ml});
	});
	
	self.addEventListener('langClicked', function(e) {
		if(lang == "mal") {
			dbResult = dbHelper.getRows("eng");
			tableData = dbResult.data;
			index = dbResult.index;
		}
		else {
			dbResult = dbHelper.getRows("mal");
			tableData = dbResult.data;
			index = dbResult.index;
		}
		
		table.setData([]);
		table.setIndex(index);
		table.setData(tableData);
		
		if(e.langButton.title == "ENG") {
			e.langButton.title = "MAL";
			lang = "eng";
		}
		else {
			e.langButton.title = "ENG";
			lang = "mal";
		}
	});
	
	var searchButton = Titanium.UI.createButton({
	    image: 'search.png'
	});
	
	var trashButton = Titanium.UI.createButton({
	    systemButton:Titanium.UI.iPhone.SystemButton.TRASH
	});
	
	var flexSpace = Titanium.UI.createButton({
	    systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});

	var noDataLabel = Titanium.UI.createLabel({
		text : 'No bookmarks',
		color : '#fff',
		width : 'auto',
		height : 'auto',
		textAlign : 'center'
	});
	
	var toolbar = Titanium.UI.iOS.createToolbar({
	    items: tableData.length > 0 ? [searchButton, flexSpace, trashButton] : [flexSpace, noDataLabel, flexSpace],
	    bottom:0,
	    borderTop:true,
	    borderBottom:false,
	    barColor: '#000',
	    opacity: 0.9
	});
	
	self.add(toolbar);
	
	searchButton.addEventListener('click', function(e) {
		bookmarkSearch.focus();
	});
	
	trashButton.addEventListener('click', function(e) {
		var dialog = Ti.UI.createAlertDialog({
		    cancel: 1,
		    buttonNames: ['Confirm', 'Cancel'],
		    message: 'Remove all bookmarks?'
		});
		
		dialog.addEventListener('click', function(e){
		    if (e.index === e.source.cancel){
		      Ti.API.info('The cancel button was clicked');
		    }
		    else {
		    	dbHelper.deleteAll();
		    	self.reloadData();
		    }
		});
	  	dialog.show();
	});
	
	self.reloadData = function() {
		dbResult = dbHelper.getRows(lang);
		tableData = dbResult.data;
		index = dbResult.index;
		table.setData([]);
		table.setIndex(index);
		table.setData(tableData);
		table.setSearch(null);
		table.setSearch(bookmarkSearch);
		if(tableData.length > 0) {
			toolbar.setItems([searchButton, flexSpace, trashButton]);
		}
		else {
			toolbar.setItems([flexSpace, noDataLabel, flexSpace]);
		}
	};
	
	return self;
};

module.exports = BookmarkView; 